/*
 * Copyright (c) 2010 Frederic Daoud
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package com.fdaoud.rayures.extension;

import static com.fdaoud.rayures.util.Constants.CONFIG_LANGUAGE_PARAM;
import static com.fdaoud.rayures.util.Constants.DEFAULT_LANGUAGE_PARAM;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import net.sourceforge.stripes.config.ConfigurableComponent;
import net.sourceforge.stripes.config.Configuration;
import net.sourceforge.stripes.localization.DefaultLocalePicker;
import net.sourceforge.stripes.util.Log;

/**
 * This {@link net.sourceforge.stripes.localization.LocalePicker} looks in the request for a
 * parameter named {@value com.fdaoud.rayures.util.Constants#DEFAULT_LANGUAGE_PARAM} from
 * which to set the {@link Locale}. You can change this parameter in {@code web.xml} like so:
 *
 * <pre>
 * &lt;filter&gt;
 *   &lt;filter-name&gt;StripesFilter&lt;/filter-name&gt;
 *   &lt;filter-class&gt;net.sourceforge.stripes.controller.StripesFilter&lt;/filter-class&gt;
 *   &lt;!-- ... --&gt;
 *   <font color="blue">
 *   &lt;init-param&gt;
 *     &lt;param-name&gt;RayuresLocalePicker.LanguageParameter&lt;/param-name&gt;
 *     &lt;param-value&gt;locale&lt;/param-value&gt;
 *   &lt;/init-param&gt;
 *   </font>
 *   &lt;!-- ... --&gt;
 * &lt;/filter&gt;
 * </pre>
 *
 * If the parameter is in the request, it is set in the session and used for further
 * requests until a different parameter is found in the request. When the parameter is neither in
 * the request nor the session, the locale from {@link DefaultLocalePicker} is used.
 *
 * <p>
 * This makes it easy to add a link that lets the user switch the language. Simply include a request
 * parameter with the desired language code, as in {@code "en"} for English and {@code "fr"} for
 * French.
 * </p>
 *
 * @author Frederic Daoud
 */
public class RayuresLocalePicker extends DefaultLocalePicker implements ConfigurableComponent {
  private static final Log log = Log.getInstance(RayuresLocalePicker.class);

  private String languageParam;

  public void init(Configuration config) throws Exception {
    super.init(config);

    languageParam = config.getBootstrapPropertyResolver().getFilterConfig()
      .getInitParameter(CONFIG_LANGUAGE_PARAM); 

    if (languageParam == null) {
      languageParam = DEFAULT_LANGUAGE_PARAM;
    }

    log.debug("Language parameter: ", languageParam);
  }

  @Override
  public Locale pickLocale(HttpServletRequest request) {
    HttpSession session = request.getSession();

    // Look in the request.
    String language = request.getParameter(languageParam);
    if (language != null && session != null) {
      session.setAttribute(languageParam, language);
      log.debug("Found language parameter in request: ", language);
    }
    // Not found in the request? Look in the session.
    else if (session != null) {
      language = (String) session.getAttribute(languageParam);
      log.debug("Language parameter in session: ", language);
    }
    // Use the language if found.
    if (language != null) {
      return new Locale(language);
    }

    // Otherwise, use the default.
    return super.pickLocale(request);
  }
}
